/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.azon.ip.utils;

/**
 *
 * @author Ahmad Fauzan
 */
public class ImageHelper {
    
    public static int[][] convertToBinaryImage(int[][] imageMatrix, boolean inverse) {
        int[][] res = new int[imageMatrix.length][imageMatrix[0].length];
        
        for(int i=0; i < res.length; i++) {
            for(int j=0; j < res[i].length;j++) {
                if(inverse) {
                    res[i][j] = imageMatrix[i][j] > 125 ? 0 : 1;
                } else
                    res[i][j] = imageMatrix[i][j] > 125 ? 1 : 0;
            }
        }
        
        return res;
    }
    
    public static int[][] convertBinaryToGrayscaleImage(int[][] binaryImage, boolean inverse) {
        int[][] res = new int[binaryImage.length][binaryImage[0].length];
        
        for(int i=0; i < res.length; i++) {
            for(int j=0; j < res[i].length;j++) {
                if(inverse) {
                    res[i][j] = binaryImage[i][j] == 0 ? 255 : 0;
                } else
                    res[i][j] = binaryImage[i][j] == 0 ? 0 : 255;
            }
        }
        
        return res;
    }
    
    public static int[][] scaleImageMatrix(int[][] imageMatrix, float scale) {
        int[][] imageResult = new int[(int)(imageMatrix.length*scale)][(int)(imageMatrix[0].length*scale)];
        
        for(int y =0; y< imageResult.length; y++) {
            for(int x = 0; x < imageResult[y].length; x++) {
                imageResult[y][x] = imageMatrix[(int)(y/scale)][(int)(x/scale)];
            }
        }
        return imageResult;
    }
    
    public static int[][] scaleImageMatrix(int[][] imageMatrix, int width, int height) {
        int[][] imageResult = new int[height][width];
        float scaleX = width/(float)imageMatrix[0].length;
        float scaleY = height/(float)imageMatrix.length;
        for(int y =0; y< imageResult.length; y++) {
            for(int x = 0; x < imageResult[y].length; x++) {
                imageResult[y][x] = imageMatrix[(int)(y/scaleY)][(int)(x/scaleX)];
            }
        }
        return imageResult;
    }
    
    public static int[][] cropMatrix(int[][] imageMatrix, int xStart, int yStart, int width, int height) {
        int[][] res = new int[height][width];
        for(int y=0; y < height;y++) {
            for(int x=0; x < width; x++ ) {
                res[y][x] = imageMatrix[yStart + y][xStart + x];
            }
        }
        return res;
    }
}
